package com.grts.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grts.pojo.MajorBean;
import com.grts.pojo.t_student;
import com.grts.service.IMajorService;
import com.grts.service.SearchService;

/**
 * 
 * @ClassName:       SearchController
 * @Description:     处理查询人数
 * @author:          杨浩
 * @date:            2018年4月5日        下午3:58:49
 */
@Controller
@RequestMapping("/search")
public class SearchController {
	
	@Autowired
	private SearchService ss;
	@Resource
	private IMajorService ms;
	/**
	 * 查询所有的学生,并以分页的方式显示
	 * @param pn
	 * @param map
	 * @return
	 */
	@RequestMapping("/students")
	public String findAll(@RequestParam(required = false,defaultValue = "1",value = "pn")Integer pn,  
            Map<String,Object> map){  
	    PageHelper.startPage(pn,5);  
	    List<t_student> students = ss.findAllStudent();    
	    PageInfo<t_student> pageInfo = new PageInfo<>(students,5);  
	    map.put("pageInfo",pageInfo); 
	    return "search"; 
	}
	/**
	 * 通过学生的编号查询所有的学生
	 * @param id
	 * @return
	 */
	@RequestMapping("/searchById")
	public ModelAndView searchById(Integer id){
		t_student student=ss.findStudent(id);
	    ModelAndView mv=new ModelAndView();
	    mv.setViewName("search2");
	    List<t_student> students =new ArrayList<t_student>();
	    students.add(student);
	    mv.addObject("students",students);
	    mv.addObject("count",students.size());
		return  mv;
	}
	/**
	 * 通过系部的名称查询该系下面的学生
	 * @param deptName
	 * @return
	 */
	@RequestMapping("searchByDept")
	public ModelAndView searchByDept(String deptName){
	    List<t_student> students =ss.searchByDept(deptName);
	    for (t_student t_student : students) {
			System.out.println(t_student);
		}
	    ModelAndView mv=new ModelAndView();
	    mv.addObject("students",students);
	    mv.addObject("count",students.size());
	    mv.setViewName("search2");
	    return mv; 
	}
	/**
	 * 根据时间查询
	 * @param day
	 * @return
	 */
	@RequestMapping("searchDay")
	public ModelAndView searchToday(Integer day){
		List<t_student> students =ss.searchDay(day);
	    ModelAndView mv=new ModelAndView();
	    mv.addObject("students",students);
	    mv.addObject("count",students.size());
	    mv.setViewName("search2");
	    return mv; 
	}
	/**
	 * 根据学制查询
	 * @param length
	 * @return
	 */
	@RequestMapping("/searchLength")
	public ModelAndView searchLength(Integer length){
		List<t_student> students =ss.searchLength(length);
	    ModelAndView mv=new ModelAndView();
	    mv.addObject("students",students);
	    mv.addObject("count",students.size());
	    mv.setViewName("search2");
	    return mv;
	    
	}
	/**
	 * 查询所有的系
	 * @param length
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/searchDeptAndMajor")
	public List<MajorBean> searchDeptAndMajor(){
		List<MajorBean>  majors = ms.queryAll();			    	    
	    return majors;
	}
	/**
	 * 点击系别传入fatherid,查询出所有的专业
	 * @param fatherid
	 * @return
	 */
	@RequestMapping("/searchMajor")
	public ModelAndView searchMajor(Integer fatherid){
		fatherid =fatherid+1;		
		List<MajorBean>  majors = ms.queryByFatherid(fatherid);
		ModelAndView mv=new ModelAndView();
		mv.setViewName("search2");
		mv.addObject("majors", majors);
		return mv;
	}
	/**
	 * 根据专业的id查询学生信息
	 * @param mid
	 * @return
	 */
	@RequestMapping("/searchStudentByMajor")
	public ModelAndView searchStudentByMajor(Integer mid){
		
		List<t_student> students=ss.searchStudentByMajor(mid);
		ModelAndView mv=new ModelAndView();
		mv.setViewName("search2");
		mv.addObject("students", students);
		mv.addObject("count",students.size());
		return mv;
	}
}
